'''
Created on Mar 24, 2014

@author: kieubinh
extract some features:
-

'''
# -*- coding: utf-8 -*-

linkInput = "data\RITE2-UnitTest\input\RITE2_JA_test_unittest\RITE2_JA_test_unittest.parsed.KNP.xml"
linkResult = "data\RITE2-UnitTest\input\RITE2_JA_test_unittest\RITE2_JA_test_unittest_result.xml"
linkOutput="data\RITE2-UnitTest\\features\\feature1_test.txt"

import sys
sys.path.append('../ReadData/')

import os
directory_path = os.getcwd()

import xml2dict

if __name__ == '__main__':  
    # get root path to ../javn/
    root_path = directory_path[:directory_path.find("javn")+5]
        
    inputFile = root_path+linkInput
    outputFile = root_path+linkOutput
    resultFile = root_path+linkResult
    
    xml = xml2dict.XML2Dict()
    
    doc = xml.parse(inputFile)
    doc2 = xml.parse(resultFile)
    print outputFile
    out = open(outputFile,'w')
    
    #print doc['dataset']['pair'][0]['t1']['value'].encode('utf8')
    #print doc['dataset']['pair'][0]['t2']['value'].encode('utf8')
    count=0
    for pair in doc['dataset']['pair']:        
        similar_t1=0
        len_t1 = len(pair['t1']['value'])
        for char_t1 in pair['t1']['value']:
            if char_t1 in pair['t2']['value']:
                similar_t1+=1
        similar_t2=0
        len_t2=len((pair['t2']['value']))
        for char_t2 in pair['t2']['value']:
            if char_t2 in pair['t1']['value']:
                similar_t2+=1
        cosine_char=1.0*min(similar_t1,similar_t2)/(len_t1*len_t2)        
        pair2 = doc2['dataset']['pair'][count]
        #print pair2['label'].value
        if pair2['label'].value=='Y':
            label = 1
        else:
            label = 0
        out.write(str(label)+" 1:"+str(cosine_char)+ " #"+ pair2['id'].value+"\n")
        count+=1
    out.close()
    
        
    
    
        